import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author YuanYuan
 * @Date 2022/6/17
 * @Time 15:21
 */
public class TestJDBCSelect {

    public static void main(String[] args) throws SQLException {

        DataSource dataSource = new MysqlDataSource();

        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/ebook?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("111111");

        Connection connection = dataSource.getConnection();
        //查询计算机分类下的图书借阅信息
        String sql = "SELECT bk.NAME book_name,bk.author book_author," +
                "s.NAME student_name,bi.start_time,bi.end_time" +
                " FROM borrow_info bi JOIN book bk ON bi.book_id = bk.id" +
                " JOIN category c ON bk.category_id = c.id" +
                " JOIN student s ON bi.student_id = s.id" +
                " WHERE c.NAME = ?";

        PreparedStatement statement = connection.prepareStatement(sql);

        ResultSet resultSet = statement.executeQuery();

        while (resultSet.next()){

            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");

            System.out.println("id = "+ id+"  name = "+name);
        }

        resultSet.close();
        statement.close();
        connection.close();

    }
}
